package com.wsc.wisdomchina.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyBulletinListBean;

import java.util.List;

public class BulletinAdapter extends BaseAdapter {

    private Context context;
    private List<PartyBulletinListBean> messageBeen;
    private LayoutInflater mInflater;

    public BulletinAdapter(Context context, List<PartyBulletinListBean> messageBeen) {
        this.context = context;
        this.messageBeen = messageBeen;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return messageBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return messageBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_quanbu, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        bindListItem(holder, position);//控制视图显示数据
        return convertView;
    }

    //控制视图显示数据
    private void bindListItem(ViewHolder holder, int pos) {
        holder.item_quanbu_total.setText(messageBeen.get(pos).getReadStatistics()+" 阅");
        holder.item_quanbu_title.setText(messageBeen.get(pos).getBulletinTitle());
        String guideImage=messageBeen.get(pos).getImages();
        if(guideImage==null || guideImage.isEmpty())
        {
            holder.item_quanbu_imageView.setVisibility(View.GONE);
        }else {
            holder.item_quanbu_imageView.setVisibility(View.VISIBLE);
            Glide.with(context).load(guideImage)
//                    .placeholder(R.mipmap.own_pic_logout)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.item_quanbu_imageView);
        }
        String date_time = Tools.timedate(String.valueOf(messageBeen.get(pos).getCreateTime() / 1000));
        holder.item_quanbu_time.setText(date_time);
    }

    public class ViewHolder {
        private TextView item_quanbu_title;//标题
        private TextView item_quanbu_total;//来源
        private TextView item_quanbu_time;//时间
        private ImageView item_quanbu_imageView;//图片

        public ViewHolder(View view) {

            item_quanbu_title = (TextView) view.findViewById(R.id.item_quanbu_title);
            item_quanbu_total = (TextView) view.findViewById(R.id.item_quanbu_total);
            item_quanbu_time = (TextView) view.findViewById(R.id.item_quanbu_time);
            item_quanbu_imageView = (ImageView) view.findViewById(R.id.item_quanbu_imageView);
        }
    }

}
